package cn.tedu.wms.passport.controller;

import cn.tedu.wms.passport.pojo.dto.AdminAddNewDTO;
import cn.tedu.wms.passport.pojo.dto.AdminLoginDTO;
import cn.tedu.wms.passport.pojo.dto.AdminUpdateDTO;
import cn.tedu.wms.passport.pojo.entity.Admin;
import cn.tedu.wms.passport.pojo.vo.AdminInfoVO;
import cn.tedu.wms.passport.pojo.vo.AdminListItemVO;
import cn.tedu.wms.passport.pojo.vo.AdminStandardVO;
import cn.tedu.wms.passport.service.IAdminService;
import cn.tedu.wms.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 *
 *
 * @author yhy@cn.tedu
 * @version 0.0.1
 */
@Slf4j
@Api(tags = "01. 管理员管理模块")
@RestController
@RequestMapping("/admins")
public class AdminController {

    @Autowired
    IAdminService adminService;

    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO) {
        log.debug("开始处理【管理员登录】的请求，参数：{}", adminLoginDTO);
        String jwt = adminService.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 60)
    @PostMapping("/logout")
    public JsonResult<Void> logout() {
        log.debug("开始处理【退出登录】的请求，");
        adminService.logout();
        return JsonResult.ok();
    }


    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(AdminAddNewDTO adminAddNewDTO) {
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除管理员")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    //@PreAuthorize("hasAuthority('/ams/admin/delete')")
    public JsonResult<Void> delete(@PathVariable Long id/*,
                                   @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal*/) {
        log.debug("开始处理【删除管理员】的请求，参数：{}", id);
        //log.debug("当前登录的当事人：{}", loginPrincipal);
        adminService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("编辑管理员")
    @ApiOperationSupport(order = 305)
    @PostMapping("/save")
    public JsonResult<Void> save(AdminUpdateDTO adminUpdateDTO) {
        log.debug("开始处理【编辑管理员】的请求，参数：{}", adminUpdateDTO);
        adminService.updateById(adminUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("启用管理员")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    //@PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用管理员】的请求，参数：{}", id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用管理员")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    //@PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用管理员】的请求，参数：{}", id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    //@PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<List<AdminListItemVO>> list() {
        log.debug("开始处理【查询管理员列表】的请求");
        List<AdminListItemVO> list = adminService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("根据id查询管理员信息")
    @ApiOperationSupport(order = 430)
    @GetMapping("/getAdminInfoById")
    public JsonResult<AdminInfoVO> getAdminInfoById(Long id) {
        log.debug("开始处理【查询管理员信息】的请求");
        AdminInfoVO adminInfoVO = adminService.getAdminInfoById(id);
        return JsonResult.ok(adminInfoVO);
    }
}
